//
//  MManager.m
//  IMChat
//
//  Created by Admin on 2021/6/30.
//  Copyright © 2021年 TeamOne. All rights reserved.
//

#import "MManager.h"

@implementation MManager
//增加User的信息
- (void)insert
{
    
    //1.取出NSManagedObjectContext.
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    //2.通过上下文,新建一个CoreData Entity模型.数据库读取这个表
    MassageEntity *message = [NSEntityDescription insertNewObjectForEntityForName:@"MassageEntity"
                                                           inManagedObjectContext:delegate.persistentContainer.viewContext];
    //3.进行属性设置
    
    
    message.classname = classname;
    message.content=content;
    message.dateTime=dateTime;
    message.msgUID=msgUID;
    
    
    ///打印
    NSLog(@"信息为 classname是:%@ content是:%@ dateTime是:%@ msgUID是:%@ ,增加成功",message.classname, message.content,message.dateTime,message.msgUID);
    
    //4.保存上下文，保存到数据库
    [delegate saveContext];
    
    
}

// 2.查找某一个User的信息(UserId)
-(void)query
{
    //1.取出NSManagedObjectContext.
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    //2.通过上下文,新建一个CoreData Entity模型.数据库读取这个表
    NSEntityDescription *message = [NSEntityDescription entityForName:@"MassageEntity"
                                               inManagedObjectContext:delegate.persistentContainer.viewContext];
    
    ///创建查询请求
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    ///设置查询请求的 实体
    [request setEntity:message];
    ///这是查询条件:UserID
    //        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"msgUID=%@",msgUID];
    //        [request setPredicate:predicate];
    
    ///获取查询的结果
    NSArray *resultArray = [delegate.persistentContainer.viewContext executeFetchRequest:request
                                                                                   error:nil];
    ///打印查询结果
    for (MassageEntity *message in resultArray) {
        NSLog(@"信息为 classname是:%@ content是:%@ dateTime是:%@ msgUID是:%@ ",message.classname, message.content,message.dateTime,message.msgUID);
    }
    NSLog(@"查询信息完成");
}


//修改：直接修改模型对象后通过context保存即可
-(void)update
{
    //1.取出NSManagedObjectContext.
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    //2.通过上下文,新建一个CoreData Entity模型.
    ///读取所有学生的实体
    NSEntityDescription *message = [NSEntityDescription entityForName:@"MassageEntity"
                                               inManagedObjectContext:delegate.persistentContainer.viewContext];
    //    //创建请求（同查，查出来才能删）
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    [request setEntity:message];
    
    ///这是查询条件:UserID
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"msgUID=%@",msgUID];
    [request setPredicate:predicate];
    
    ///获取符合条件的结果
    NSArray *resultArray = [delegate.persistentContainer.viewContext executeFetchRequest:request
                                                                                   error:nil];
    if (resultArray.count>0) {
        for (MassageEntity *message in resultArray) {
            ///把名字改为李四
            message.classname= @"李四";
            ///并且把名字添加一个修
            message.classname = [NSString stringWithFormat:@"%@修",message.classname];
        }
        ///保存结果并且打印
        [delegate saveContext];
        NSLog(@"修改信息完成");
    }else{
        NSLog(@"没有符合条件的结果");
    }
    
    
    
}


//删除一个实体对象
-(void)datadelete
{
    //1.取出NSManagedObjectContext.
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    //2.通过上下文,新建一个CoreData Entity模型.
    ///读取所有学生的实体
    NSEntityDescription *message = [NSEntityDescription entityForName:@"MassageEntity"
                                               inManagedObjectContext:delegate.persistentContainer.viewContext];
    //创建请求（同查，查出来才能删）
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    [request setEntity:message];
    
    //这是查询条件:UserID
    //    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"msgUID=%@",msgUID];
    //    [request setPredicate:predicate];
    ///获取符合条件的结果
    NSArray *resultArray = [delegate.persistentContainer.viewContext executeFetchRequest:request
                                                                                   error:nil];
    if (resultArray.count>0) {
        for (MassageEntity *message in resultArray) {
            ///删除实体
            [delegate.persistentContainer.viewContext deleteObject:message];
        }
        ///保存结果并且打印
        [delegate saveContext];
        NSLog(@"删除朋友完成");
    }else{
        NSLog(@"没有符合条件的结果");
    }
    
    
    
    
}
@end
